<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="metronic/assets/global/plugins/jquery-1.11.0.min.js" type="text/javascript"></script>
    <link href="metronic/assets/global/plugins/bootstrap/css/bootstrap.min.css" type="text/css"/>
    <link href="metronic/assets/global/css/components.css" rel="stylesheet" type="text/css"/>
    <link href="metronic/assets/admin/pages/css/login.css" rel="stylesheet" type="text/css"/>

    <style>
        img{
            width: 200px;
            height: auto;
        }
    </style>
</head>

<body >
<div class="content">
    <h3 class="form-title">商品浏览</h3>
   <div class="table-responsive">
       <table class="table">
           <thead><tr>
               <th>商品名</th>
               <th>商品图片</th>
               <th>商品描述</th>
               <th>商品价格</th>
               <th>商品库存</th>
               <th>商品销量</th>
           </tr></thead>
           <tbody id="mytable">

           </tbody>
       </table>
   </div>
</div>
<script>
    var items = [];
    jQuery(document).ready(function () {


            $.ajax({
                type:"GET",

                url:"http://localhost:8080/item/listItem",
                  xhrFields:{withCredentials: true},
                success:function (data) {
                    if (data.status == "success") {
                        items=data.data;
                        reloadDom();
                    }else{
                        alert("获取商品列表失败，原因是"+data.data.errMsg)
                    }
                },
                error:function (data) {  //网络中断时被调用
                    alert("获取商品列表失败，原因是与服务器网络中断或者"+data.responseText)
                }
            });


    });

    function reloadDom() {
        for (var i = 0; i < items.length; i++) {
            var item = items[i];
            var dom="<tr data-id='"+item.id+"' id='itemDetail"+item.id+"'><td>"+item.title+
             
                "</td><td><img src='"+item.imgUrl+"' /></td>"+
                "</td><td>"+item.description+"</td>"+
                "</td><td>"+item.price+"</td>"+
                "</td><td>"+item.stock+"</td>"+
                "</td><td>"+item.sales+"</td>"+
                "</tr>"

            $("#mytable").append($(dom))

            $("#itemDetail"+item.id).on("click",function (e) {
                window.location.href="getItem.html?id="+$(this).data("id")
            })
        }

    }
</script>
</body>
</html>